<?php

namespace App\Services;

use App\Repositories\Contracts\MeetDayRepository;
use App\Repositories\Contracts\VenueRepository;
use Carbon\Carbon;

class VenueService
{
    private $venueRepository;
    private $meetDayRepository;

    public function __construct(VenueRepository $venueRepository, MeetDayRepository $meetDayRepository)
    {
        $this->venueRepository = $venueRepository;
        $this->meetDayRepository = $meetDayRepository;
    }

    /**
     * 获取列表
     *
     * @param array|null $param
     */
    public function getList($param = null)
    {
        try {
            $filter = [];
            $result = $this->venueRepository->paginate($param['per_page'] ?? null, $filter);
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    public function getAll()
    {
        try {
            $filter = [];

            $result = $this->venueRepository->getAll($filter);

            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }

    }

    /**
     * 获取详情
     *
     * @param int $id
     * @return object|array
     */
    public function getInfo($id)
    {
        try {
            //场馆信息
            $venueInfo = $this->venueRepository->find($id);

            // 今天+10天
            $endDay = Carbon::today()->addDays(6);

            //日期范围
            $filter = [
                'start_day' => Carbon::today(),
                'end_day' => $endDay,
                'venue_id' => $id,
            ];
            $day = $this->meetDayRepository->findByFilter($filter);

            $dayData = $day['data'];
            for ($i = 0; $i < count($dayData); $i++) {
                // 最后一天
                $lastDay = Carbon::parse($dayData[count($dayData) - 1]['day']);
                if ($lastDay->lt($endDay)) {
                    // 比7天少, 补全
                    array_push($dayData, [
                        'day' => $lastDay->addDay(1)->format('Y-m-d'),
                        'status' => 1,
                        'hours' => [],
                    ]);
                }
            }

            $result = [
                'venueInfo' => $venueInfo,
                'day' => $dayData,
            ];
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 新增
     *
     * @param array $param
     */
    public function add($param)
    {
        try {
            // $param['info'] = "\"" . addslashes(json_encode($param['info'])) . "\"";
            $param['info'] = json_encode($param['info']);

            $venue = $this->venueRepository->store($param);
            return $venue;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 修改
     *
     * @param array $param
     * @param int $id
     */
    public function update($param, $id)
    {
        try {
            $venue = $this->venueRepository->update($param, $id);
            return $venue;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 删除
     *
     * @param int $id
     */
    public function delete($id)
    {
        try {
            $venue = $this->venueRepository->delete($id);

            $result = [];
            if ($venue) {
                $result = 'success';
            } else {
                $result = 'error';
            }
            return $result;

        } catch (\Throwable $th) {
            throw $th;
        }
    }
}
